package com.example.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.user.dao.ApUserFanDao;
import com.example.user.dao.ApUserFollowDao;
import com.example.user.feign.ArticleFeign;
import com.example.user.service.AuthorService;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.FollowUserDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFan;
import com.usian.model.user.pojos.ApUserFollow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 作者服务impl
 *
 */
@Service
public class AuthorServiceImpl implements AuthorService {
    @Autowired
    ApUserFanDao apUserFanDao;
    @Autowired
    ApUserFollowDao apUserFollowDao;
    @Autowired
    ArticleFeign articleFeign;
    @Autowired
    RedisTemplate redisTemplate;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult follow(FollowUserDto followUserDto) {
        //参数校验
        if (followUserDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApAuthor author = articleFeign.getAuthorById(followUserDto.getAuthorId());
        //服务调用获取作者的信息
        if (author==null){
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //作者的id
        Integer authorId = author.getUserId();
        //获取关联表信息，如果有说明已经关注
        LambdaQueryWrapper<ApUserFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUserFollow::getUserId,authorId).eq(ApUserFollow::getFollowId,followUserDto.getUserId());
        ApUserFollow apUserFollow = apUserFollowDao.selectOne(queryWrapper);
        if (followUserDto.getOperation()==1){
            if (apUserFollow==null){
                //关注
                //首先获取登录用户的信息  并且保存到Ap_user_fan
                String apUserJson = (String) redisTemplate.opsForValue().get(followUserDto.getUserId());
                ApUser apUser = JSONObject.parseObject(apUserJson, ApUser.class);
                ApUserFan apUserFan=new ApUserFan();
                apUserFan.setUserId(authorId);
                apUserFan.setFansId(apUser.getId().longValue());
                apUserFan.setFansName(apUser.getName());
                apUserFan.setCreatedTime(new Date());
                apUserFanDao.insert(apUserFan);
                //保存到关联表ap_user_follow
                ApUserFollow apUserFollow1=new ApUserFollow();
                //set值
                
                
                
                
                apUserFollowDao.insert(apUserFollow1);
                //followAuthor();
             
            
            }else {
                return ResponseResult.errorResult(AppHttpCodeEnum.FOLLOWED);
            }
        }else if (followUserDto.getOperation()==0){
            //取消关注
            LambdaQueryWrapper<ApUserFan> wrapper = new LambdaQueryWrapper<ApUserFan>();
            wrapper.eq(ApUserFan::getUserId,authorId).eq(ApUserFan::getFansId,followUserDto.getUserId());
            apUserFanDao.deleteById(wrapper);
            //删除关联表数据
            LambdaQueryWrapper<ApUserFollow> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ApUserFollow::getUserId,followUserDto.getUserId()).eq(ApUserFollow::getFollowId,authorId);
            apUserFollowDao.delete(queryWrapper1);
            return ResponseResult.okResult("");
    
            //followUserCancle();
        }
        
        return ResponseResult.okResult("");
    }
    
    public void followAuthor(Integer authorId){
    
    }
}
